/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.webhomesites.locacao.presenter;

import br.com.webhomesites.locacao.contracts.IManutencaoPresenter;
import br.com.webhomesites.locacao.contracts.IManutencaoView;
import br.com.webhomesites.locacao.model.*;
import br.com.webhomesites.locacao.presenter.EmpresaManutencaoPresenterActionListeners.SairActionListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.BuscaAvancadaActionlistener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.CampoKeyListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.CancelarItemManutencaoActionListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.CancelarManutencaoActionListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.ComboStateListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.InserirItemManutencaoActionListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.InserirManutencaoActionListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.RemoverItemManutencaoActionListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.SalvarItemManutencaoActionListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.SalvarManutencaoActionListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.TableItensManutencaoMouseListener;
import br.com.webhomesites.locacao.presenter.ManutencaoPresenterActionListeners.TableManutencaoMouseListener;
import br.com.webhomesites.locacao.view.BuscarManutencaoView;
import br.com.webhomesites.locacao.view.ManutView;
import br.com.webhomesites.padrao.model.ProdutoDAO;
import br.com.webhomesites.padrao.model.ProdutoModel;
import br.com.webhomesites.padrao.utils.Strategy;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Webhome
 */
public class ManutencaoPresenter implements IManutencaoPresenter {

    private ManutencaoModel manutencaoModel;
    private ManutencaoDAO manutencaoDAO;
    private ManutView manutencaoView;
    private ManutencaoBO manutencaoBO;
    private ItemManutencaoModel itemManutencaoModel;
    private ItemManutencaoDAO itemManutencaoDAO;
    private EmpresaManutencaoModel empresaManutencaoModel;
    private ProdutoModel produtoModel;
    private ProdutoDAO produtoDAO;
    private EmpresaManutencaoDAO empresaManutencaoDAO;
    
    List<ItemManutencaoModel> listaItens = new ArrayList<>();
    List<ManutencaoModel> listaManutencao = new ArrayList<>();
    public final Strategy INSERT_STRATEGY = new InsertStrategy();
    public final Strategy INSERT_ITEM_STRATEGY = new InsertItemStrategy();
    public final Strategy REMOVE_ITEM_STRATEGY = new RemoveItemStrategy();
    public final Strategy UPDATE_STRATEGY = new UpdateStrategy();
    private Strategy operacao = INSERT_STRATEGY;
    
    DecimalFormat df = new DecimalFormat("###,###,###.00");
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

    public ManutencaoPresenter(ManutencaoModel manutencaoModel, ManutencaoDAO manutencaoDAO, ManutView manutencaoView, ItemManutencaoModel itemManutencaoModel, ItemManutencaoDAO itemManutencaoDAO, EmpresaManutencaoModel empresaManutencaoModel, ProdutoModel produtomodel, ProdutoDAO produtoDAO, EmpresaManutencaoDAO empresaManutencaoDAO) {
        this.manutencaoModel = manutencaoModel;
        this.manutencaoDAO = manutencaoDAO;
        this.manutencaoView = manutencaoView;

        this.itemManutencaoModel = itemManutencaoModel;
        this.itemManutencaoDAO = itemManutencaoDAO;
        this.empresaManutencaoModel = empresaManutencaoModel;
        this.produtoModel = produtomodel;
        this.produtoDAO = produtoDAO;
        this.empresaManutencaoDAO = empresaManutencaoDAO;

        manutencaoBO = new ManutencaoBO();
    }

    @Override
    public void setUpViewListeners() {

        manutencaoView.setRemoverItemManutencaoActionListener(new RemoverItemManutencaoActionListener(this));
        manutencaoView.setInserirManutencaoActionListener(new InserirManutencaoActionListener(this));
        manutencaoView.setAlterarActionListener(new ManutencaoPresenterActionListeners.AlterarActionListener(this));
        manutencaoView.setCancelarManutencaoActionListener(new CancelarManutencaoActionListener(this));
        manutencaoView.setCancelarItemManutencaoActionListener(new CancelarItemManutencaoActionListener(this));
        manutencaoView.setSalvarManutencaoActionListener(new SalvarManutencaoActionListener(this));
        manutencaoView.setSalvarItemManutencaoActionListener(new SalvarItemManutencaoActionListener(this));
        manutencaoView.setInserirItemManutencaoActionListener(new InserirItemManutencaoActionListener(this));
        manutencaoView.setBtnSairActionListener(new SairActionListener());
        manutencaoView.setTableItensManutencaoMouseListener(new TableItensManutencaoMouseListener(this));
        manutencaoView.setTableManutencaoMouseListener(new TableManutencaoMouseListener(this));
        

        manutencaoView.setMascaraListener(new CampoKeyListener(this));

        manutencaoView.setComboDescricaoProdutoItemStateChanged(new ComboStateListener(this));
        manutencaoView.setBuscaAvancadaActionListener(new BuscaAvancadaActionlistener(this));

    }

    @Override
    public void updateModelManutencaoFromView() {
        try {
            DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            manutencaoModel.setId(manutencaoView.getCodigoManutencao());
            manutencaoModel.setNomeEmpresaManutencao(manutencaoView.getNomeEmpresaManutencao());
            manutencaoModel.setDataSaidaManutencao((Date) formatter.parse(manutencaoView.getDataSaidaManutencao()));
            manutencaoModel.setDataPrevDevolucao((Date) formatter.parse(manutencaoView.getDataPrevDevolucao()));
            manutencaoModel.setDataDevolucao(null);
            manutencaoModel.setStatus("A");
            manutencaoModel.setValorTotal(0.00);
            manutencaoModel.setValorPagar(0.00);
            manutencaoModel.setDesconto(0.00);
            empresaManutencaoModel = (empresaManutencaoDAO.findByDescricaoUnica(manutencaoView.getNomeEmpresaManutencao()));
            manutencaoModel.setEmpresaManutencaoModel(empresaManutencaoModel);
        } catch (Exception e) {
        }

        manutencaoDAO.save(manutencaoModel);

    }

    @Override
    public void updateViewManutencaoFromModel() {
        if (manutencaoModel != null) {
            manutencaoView.setListaEmpresaManutencao(empresaManutencaoDAO.findAll());
            
            manutencaoView.setCodigoManutencao(manutencaoModel.getId());
            manutencaoView.setNomeEmpresaManutencao(manutencaoModel.getEmpresaManutencaoModel().getNome());
            manutencaoView.setDataSaidaManutencao(formatter.format(manutencaoModel.getDataSaidaManutencao()));
            manutencaoView.setDataPrevDevolucao(formatter.format(manutencaoModel.getDataPrevDevolucao()));
            
        } else {
            manutencaoView.clearFields();
        }
    }

    @Override
    public void updateModelItemFromView() {

        produtoModel = produtoDAO.findByDescricaoUnica(manutencaoView.getDescricaoItemManutencao());
        itemManutencaoModel.setId(itemManutencaoDAO.getMaxId() + 1);
        itemManutencaoModel.setSerieItem(manutencaoView.getSerieItemManutencao());
        itemManutencaoModel.setDescricaoItem(manutencaoView.getDescricaoItemManutencao());
        itemManutencaoModel.setDataDevolucao(null);
        itemManutencaoModel.setStatusItem("A");
        itemManutencaoModel.setProblema(manutencaoView.getProblemaItemManutencao());
        itemManutencaoModel.setProdutoModel(produtoModel);
        itemManutencaoModel.setManutencaoModel(manutencaoModel);
        itemManutencaoDAO.save(itemManutencaoModel);

        //manutencaoBO.calcularTotalPositivoManutencao(this);

    }

    @Override
    public void updateModelListaMontadoItemFromView() {
//        List<ItemComposicaoProdutoModel> itensComposicaoProdutoModel = itemComposicaoProdutoDAO.findByIdComposicaoProduto(manutencaoView.getCodigoProduto());
//
//        composicaoProdutoModel = composicaoProdutoDAO.getById(manutencaoView.getCodigoProduto());
//
//        for (ItemComposicaoProdutoModel itemComposicao : itensComposicaoProdutoModel) {
//            itemManutencaoModel.setId(itemManutencaoDAO.getMaxId() + 1);
//            itemManutencaoModel.setSerieItem(itemComposicao.getProdutoModel().getSerie());
//            itemManutencaoModel.setDescricaoItem(itemComposicao.getProdutoModel().getDescricao());
//            itemManutencaoModel.setQuantidade(itemComposicao.getQuantidade());
//            itemManutencaoModel.setDataDevolucao(null);
//            itemManutencaoModel.setStatusItem("A");
//            itemManutencaoModel.setProdutoModel(itemComposicao.getProdutoModel());
//            itemManutencaoModel.setManutencaoModel(manutencaoModel);
//            itemManutencaoDAO.save(itemManutencaoModel);
//
//
//        }
//
//        //manutencaoModel.setValorTotal(manutencaoView.getValorItemManutencao()+manutencaoModel.getValorTotal());
//        //manutencaoModel.setValorPagar(manutencaoModel.getValorTotal());
//        manutencaoDAO.update(manutencaoModel);
//        //manutencaoView.setValorTotalManutencao(df.format(manutencaoModel.getValorTotal()));
    }

    @Override
    public void updateModelListaInclusoItemFromView() {
//        List<ItemComposicaoProdutoModel> itensComposicaoProdutoModel = itemComposicaoProdutoDAO.findByIdComposicaoProduto(manutencaoView.getCodigoProduto());
//
//        composicaoProdutoModel = composicaoProdutoDAO.getById(manutencaoView.getCodigoProduto());
//
//        for (ItemComposicaoProdutoModel itemComposicao : itensComposicaoProdutoModel) {
//            System.out.println("Passando pelo for");
//            itemManutencaoModel.setId(itemManutencaoDAO.getMaxId() + 1);
//            itemManutencaoModel.setSerieItem(itemComposicao.getProdutoModel().getSerie());
//            itemManutencaoModel.setDescricaoItem(itemComposicao.getProdutoModel().getDescricao());
//            itemManutencaoModel.setQuantidade(itemComposicao.getQuantidade());
//            itemManutencaoModel.setDataDevolucao(null);
//            itemManutencaoModel.setStatusItem("A");
//            itemManutencaoModel.setProdutoModel(itemComposicao.getProdutoModel());
//            itemManutencaoModel.setManutencaoModel(manutencaoModel);
//            itemManutencaoDAO.save(itemManutencaoModel);
//            
//        }
    }

    @Override
    public void updateViewItemFromModel() {
        if (itemManutencaoModel != null) {
            manutencaoView.setListaProduto(produtoDAO.findAll());
            
            manutencaoView.setCodigoProduto(itemManutencaoModel.getId());
            manutencaoView.setSerieItemManutencao(itemManutencaoModel.getSerieItem());
            manutencaoView.setDescricaoItemManutencao(itemManutencaoModel.getDescricaoItem());
            manutencaoView.setProblemaItemManutencao(itemManutencaoModel.getProblema());
        } else {
            manutencaoView.clearFields();
        }
    }

    @Override
    public void updateViewItemFromProdutoModel(ProdutoModel produtoModel) {
        if (manutencaoModel != null) {
            manutencaoView.setCodigoItemManutencao(produtoModel.getId());
            manutencaoView.setSerieItemManutencao(produtoModel.getSerie());
            //manutencaoView.setListaProduto(produtoModel.getlistaProdutos()); 
            //manutencaoView.setDescricaoItemManutencao(produtoModel.getDescricaoItem());

            //manutencaoView.setValorItemManutencao(manutencaoView.getValorProdutoManutencao() * manutencaoView.getDiasManutencao());
        } else {
            manutencaoView.clearFields();
        }
    }

    @Override
    public void updateModelFromJTable() {
        ItensManutencaoTableModel itensManutencaoTableModel = manutencaoView.getitensManutencaoTableModel();

        itemManutencaoModel = itensManutencaoTableModel.getItemManutencaoModel(manutencaoView.linhaSelecionadaTableItens());

        if (itemManutencaoModel != null) {
            this.updateViewItemFromModel();

            manutencaoView.enableBtnRemoverItem(true);
            //view.enableBtnRemover(true);  
        } else {
            //view.enableBtnAlterar(false);  
            manutencaoView.enableBtnRemoverItem(false);
        }

        this.updateViewItemFromModel();
    }
    
    public void updateTableManutencaoFromBusca(List<ManutencaoModel> listaManutencaoModel) {
        manutencaoView.setManutencaoTableModel(new ManutencaoTableModel(listaManutencaoModel));
        manutencaoView.refreshTableManutencao();
    }
    
    public void updateTableitemFromManutencaoModel(ManutencaoModel manutencaoModel) {
        manutencaoView.setItensManutencaoTableModel(new ItensManutencaoTableModel(itemManutencaoDAO.findByIdManutencao(manutencaoModel.getId())));
        manutencaoView.refreshTableItensManutencao();
    }
    
    
     public void updateModelManutencaoFromJTable() {
        ManutencaoTableModel manutencaoTableModel = manutencaoView.getManutencaoTableModel();

        manutencaoModel = manutencaoTableModel.getManutencaoModel(manutencaoView.linhaSelecionadaTableManutencao());

        if (manutencaoModel != null && !manutencaoModel.getStatus().equals("D")) {
            System.out.println("A manutenção não está nula!");
            this.updateViewManutencaoFromModel();
            
            System.out.println("O Problema está aqui!");         
            
            this.manutencaoView.setItensManutencaoTableModel(new ItensManutencaoTableModel(itemManutencaoDAO.findByIdManutencao(manutencaoModel.getId())));
            this.manutencaoView.setListaEmpresaManutencao(empresaManutencaoDAO.findAll());
            this.manutencaoView.setNomeEmpresaManutencao(manutencaoModel.getnomeEmpresaManutencao());
            //devolucaoView.enableBtnRemoverItem(true);  
            //view.enableBtnRemover(true);  
        } else {
            JOptionPane.showMessageDialog(manutencaoView, "Esta manutenção já foi devolvida!");
            //view.enableBtnAlterar(false);  
            //locacaoView.enableBtnRemoverItem(false);  
        }

        this.updateViewManutencaoFromModel();
    }

    @Override
    public void novoManutencao() {

        manutencaoModel = new ManutencaoModel();
        System.out.println("Passando em nova locação");
        manutencaoView.clearFieldsPainelManutencao();
        manutencaoView.setCodigoManutencao(manutencaoDAO.getMaxId() + 1);
        manutencaoView.setListaEmpresaManutencao(empresaManutencaoDAO.findAll());
        manutencaoView.setItensManutencaoTableModel(new ItensManutencaoTableModel(null));
        manutencaoView.setManutencaoTableModel(new ManutencaoTableModel(null));

    }

    @Override
    public void novoItemManutencao() {
        itemManutencaoModel = new ItemManutencaoModel();
        manutencaoView.clearFieldsPainelIncluirProdutos();
        manutencaoView.setListaProduto(produtoDAO.findAll());
    }

    @Override
    public void inserirManutencao() {
        this.updateModelManutencaoFromView();
        manutencaoView.clearFields();

    }

    @Override
    public void inserirItemManutencao() {

        produtoModel = produtoDAO.findByDescricaoUnica(manutencaoView.getDescricaoItemManutencao());
        //composicaoProdutoModel = composicaoProdutoDAO.getById(manutencaoView.getCodigoProduto());

        manutencaoBO.tratarInserirItem(this, produtoModel);
        manutencaoView.setItensManutencaoTableModel(new ItensManutencaoTableModel(itemManutencaoDAO.findByIdManutencao(manutencaoModel.getId())));
        manutencaoView.refreshTableItensManutencao();

    }

    @Override
    public void alterarManutencao() {
        this.updateModelManutencaoFromView();
    }

    @Override
    public void cancelarManutencao() {
        this.updateModelManutencaoFromView();
        this.updateModelItemFromView();
    }

    @Override
    public void removerItemManutencao() {

        itemManutencaoDAO.delete(itemManutencaoModel);
        manutencaoView.setItensManutencaoTableModel(new ItensManutencaoTableModel(itemManutencaoDAO.findByIdManutencao(manutencaoModel.getId())));
        manutencaoView.refreshTableItensManutencao();
        double valorTotal = 0;
        manutencaoModel.setValorTotal(valorTotal);
        manutencaoModel.setValorPagar(valorTotal);
        manutencaoDAO.update(manutencaoModel);
        manutencaoView.clearFieldsPainelIncluirProdutos();

    }      

    @Override
    public void habilitarEdicao(boolean arg) {
      
    }

    public void buscaAvancada() {

        BuscarManutencaoModel buscarManutencaoModel = new BuscarManutencaoModel();

        BuscarManutencaoView buscarManutencaoView = new BuscarManutencaoView(javax.swing.JOptionPane.getFrameForComponent(manutencaoView), true);
        
        
        BuscarManutencaoPresenter buscarManutencaoPresenter = new BuscarManutencaoPresenter(buscarManutencaoView, buscarManutencaoModel, manutencaoDAO, manutencaoModel, this);
        buscarManutencaoPresenter.setBuscador(buscarManutencaoPresenter.BUSCADOR_MANUTENCAO_VIEW);
        buscarManutencaoPresenter.createView();

    }
    
    
    @Override
    public IManutencaoView getManutencaoView() {
        return manutencaoView;
    }

    @Override
    public ManutencaoModel getManutencaoModel() {
        return manutencaoModel;
    }

    @Override
    public void setManutencaoView(ManutView manutencaoView) {
        this.manutencaoView = manutencaoView;
    }

    @Override
    public void setManutencaoModel(ManutencaoModel model) {
        this.manutencaoModel = model;
    }

    @Override
    public ManutencaoBO getManutencaoBO() {
        return manutencaoBO;
    }

    @Override
    public void setManutencaoBO(ManutencaoBO manutencaoBO) {
        this.manutencaoBO = manutencaoBO;
    }

    @Override
    public void setItemManutencaoDAO(ItemManutencaoDAO itemManutencaoDAO) {
        this.itemManutencaoDAO = itemManutencaoDAO;
    }

    @Override
    public ItemManutencaoDAO getItemManutencaoDAO() {
        return itemManutencaoDAO;
    }

    @Override
    public void setManutencaoDAO(ManutencaoDAO manutencaoDAO) {
        this.manutencaoDAO = manutencaoDAO;
    }

    @Override
    public ManutencaoDAO getManutencaoDAO() {
        return manutencaoDAO;
    }

    @Override
    public void setItemManutencaoModel(ItemManutencaoModel itemManutencaoModel) {
        this.itemManutencaoModel = itemManutencaoModel;
    }

    @Override
    public ItemManutencaoModel getItemManutencaoModel() {
        return itemManutencaoModel;
    }

    public List<ManutencaoModel> getListaManutencao() {
        return listaManutencao;
    }

    public void setListaManutencao(List<ManutencaoModel> listaManutencao) {
        this.listaManutencao = listaManutencao;
    }        

    @Override
    public void createView() {
        //this.novoManutencao();            
        this.setUpViewListeners();

        manutencaoView.setManutencaoTableModel(new ManutencaoTableModel(null));
        
        manutencaoView.removeAllitensComboDescricaoProduto();
        manutencaoView.removeAllitensComboNomeEmpresaManutencao();
        //manutencaoView.setListaEmpresaManutencao(empresaManutencaoDAO.findAll());        
        manutencaoView.setItensManutencaoTableModel(new ItensManutencaoTableModel(null));
        manutencaoView.enableBtnCancelarManutencao(false);
        manutencaoView.enableBtnSalvarManutencao(false);
        manutencaoView.enableBtnCancelarItem(false);
        manutencaoView.enableBtnSalvarItem(false);
        manutencaoView.enableBtnRemoverItem(false);
        manutencaoView.enableBtnInserirItem(false);

        manutencaoView.packAndShow();
    }

    public void setOperacao(Strategy operacao) {
        this.operacao = operacao;
    }

    public Strategy getOperacao() {
        return operacao;
    }

    private class InsertStrategy implements Strategy {

        @Override
        public void execute() {
            inserirManutencao();
        }
    }

    private class RemoveItemStrategy implements Strategy {

        @Override
        public void execute() {
            removerItemManutencao();
        }
    }

    private class InsertItemStrategy implements Strategy {

        @Override
        public void execute() {
            inserirItemManutencao();
        }
    }

    private class UpdateStrategy implements Strategy {

        @Override
        public void execute() {
            alterarManutencao();
        }
    }
}
